********************************************************************************
* PRE-TRENDS
********************************************************************************
clear
use "$data/fiscal.dta", clear
set more off

* KEEP NATIVES
drop if native==.

* DROP RURAL AREAS
drop if area ==.


* GLOBAL CONTROLS
global indctrl sex age c.age#c.age educ
global msactrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13

* REPLACE VARIABLES TO THEIR 2013 LEVEL
foreach var of varlist feff sh_pgds {
gen `var'_ = `var' if year==2013
bys area: egen `var'_t = mean(`var'_)
replace `var' = `var'_t
drop `var'_ `var'_t
	}
replace labinc13 = log(labinc13)

* DEFLATE CONTRIBUTIONS	
local variables totnet_all totnet_excl

foreach l of local variables {
replace `l' = `l' / deflactor
}

* EXPRESS CONTRIBUTIONS IN MILLIONS
foreach l of local variables{
replace `l' = `l'/ 1000000
}

* TRIM
local variables totnet_all totnet_excl
foreach l of local variables{
bys year: egen p1 = pctile(`l'), p(1)
bys year: egen p99 = pctile(`l'), p(99)
gen trim_`l' = 1 if `l'<=p1 | `l'>=p99
replace trim_`l' = 0 if trim_`l'==.
drop p1 p99
}

*****************
*** IN LEVELS ***
*****************

* DEFINE VARIABLES
global y1 	totnet_all
global y2 	totnet_excl
global x 	sh_ven2005
global z	IV2005
global w 	fex12

* RUN REGRESSIONS
levelsof year, local(levels) 
foreach l of local levels {
qui reg $y1 $x $indctrl $msactrl [pw = $w] if year==`l' & trim_totnet_all==0, cluster(area) 
display(e(df_m))
parmest, saving("$data/trends_`l'", replace)

preserve
use "$data/trends_`l'", clear
keep if parm == "$x"
gen low = estimate - 1.96 * stderr 
gen high = estimate + 1.96 * stderr
gen year = `l'
save "$data/trends_`l'", replace
restore
}
 
preserve
clear 
append using "$data/trends_2013" "$data/trends_2014" "$data/trends_2015" "$data/trends_2016" "$data/trends_2017" "$data/trends_2018"
save "$data/trends", replace
restore

levelsof year, local(levels) 
foreach l of local levels {
qui reg $y2 $x $indctrl $msactrl [pw = $w] if year==`l' & trim_totnet_excl==0, cluster(area) 
display(e(df_m))
parmest, saving("$data/trends2_`l'", replace)

preserve
use "$data/trends2_`l'", clear
keep if parm == "$x"
gen low = estimate - 1.96 * stderr 
gen high = estimate + 1.96 * stderr
gen year = `l'
save "$data/trends2_`l'", replace
restore
}
 
preserve
clear 
append using "$data/trends2_2013" "$data/trends2_2014" "$data/trends2_2015" "$data/trends2_2016" "$data/trends2_2017" "$data/trends2_2018"
save "$data/trends2", replace
restore

preserve
clear
use "$data/trends"
gen gr = 1
append using "$data/trends2"
replace gr = 2 if gr==.
sort year gr
gen x = _n
replace x = _n + 2 if _n >= 3
replace x = _n + 4 if _n >= 5
replace x = _n + 6 if _n >= 7
replace x = _n + 8 if _n >= 9
replace x = _n + 10 if _n >= 11
separate estimate, by(gr)
separate high, by(gr)
separate low, by(gr)
save "$data/pretrend", replace
restore

preserve
clear 
use "$data/pretrend"
twoway (scatter estimate1 x, mcolor(navy)) (rcap high1 low1 x, lcolor(navy)) ///
	   (scatter estimate2 x, msymbol(d) mcolor(maroon)) (rcap high2 low2 x, lcolor(maroon)), ///
	   ylabel(, angle(0)) scheme(lean1) xlabel(none) xtitle(" ") ///
	   ytitle("") yline(0, lcolor(black)) ///
	   text(-3.6 0.8 "2013", place(e)) ///
	   text(-3.6 4.8 "2014", place(e)) ///
	   text(-3.6 8.8 "2015", place(e)) ///
	   text(-3.6 12.8 "2016", place(e)) ///
	   text(-3.6 16.8 "2017", place(e)) ///
	   text(-3.6 20.8 "2018", place(e)) ///
	   legend(order(1 "All budget" 3 "Restricted") position(1) row(1) ring(0)) ///
	   ylabel(, nogrid) graphregion(color(white))
restore

******************
*** IN CHANGES ***
******************

set more off

* RESIDUALIZE CONTRIBUTIONS OF NATIVES
local variables totnet_all totnet_excl

levelsof year, local(levels) 
foreach l of local variables{
gen `l'_res = .
foreach i of local levels {
qui reg `l' $indctrl [pw = fex12] if trim_`l'==0 & year==`i', cluster(area)
predict res`l'_`i' if trim_`l'==0 & year==`i', residuals
replace `l'_res = res`l'_`i' if trim_`l'==0 & year==`i'

qui reg `l' sex age c.age#c.age [pw = fex12] if trim_`l'==0 & year==`i', cluster(area)
predict res_`l'_`i' if trim_`l'==0 & year==`i', residuals
replace `l'_res = res_`l'_`i' if res`l'==. & trim_`l'==0 & year==`i'

drop res`l'_`i' res_`l'_`i'
}
rename trim_`l' trim_`l'_res
}

* AGGREGATE DATA AT THE MSA LEVEL FOR NATIVES
local vars totnet_all_res totnet_excl_res
foreach l of local vars{
bysort area year: egen double mean_`l' = total(`l' * fex12) if trim_`l'==0
bysort area year: egen double den`l' = total(fex12) if trim_`l'==0
replace `l' = mean_`l'/den`l' 
drop mean_`l' den`l'
}

collapse (mean) `vars' sh_ven2005 IV2005 totpop13_wa $msactrl, by(area year)

* CREATE CHANGE IN DEPENDENT VARIABLES
sort area year
foreach l of local vars{
bys area: gen `l'_d = `l' - `l'[_n-1]
}
drop `vars'


global yyctrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13 

* DEFINE VARIABLES
global y1 	totnet_all_res_d
global y2 	totnet_excl_res_d
global x 	sh_ven2005
global z	IV2005
global w 	totpop13_wa

* YEARLY ESTIMATES
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui reg $y1 $x $yyctrl [aw=$w] if year==`l', robust
display(e(df_m))
parmest, saving("$data/trendagg_`l'", replace)

preserve
use "$data/trendagg_`l'", clear
keep if parm == "$x"
gen low = estimate - 1.96 * stderr 
gen high = estimate + 1.96 * stderr
gen year = `l'
save "$data/trendagg_`l'", replace
restore
}
 
preserve
clear 
append using "$data/trendagg_2014" "$data/trendagg_2015" "$data/trendagg_2016" "$data/trendagg_2017" "$data/trendagg_2018"
save "$data/trendagg", replace
restore

levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui reg $y2 $x $yyctrl [aw=$w] if year==`l', robust
display(e(df_m))
parmest, saving("$data/trendagg2_`l'", replace)

preserve
use "$data/trendagg2_`l'", clear
keep if parm == "$x"
gen low = estimate - 1.96 * stderr 
gen high = estimate + 1.96 * stderr
gen year = `l'
save "$data/trendagg2_`l'", replace
restore
}
 
preserve
clear 
append using "$data/trendagg2_2014" "$data/trendagg2_2015" "$data/trendagg2_2016" "$data/trendagg2_2017" "$data/trendagg2_2018"
save "$data/trendagg2", replace
restore

preserve
clear
use "$data/trendagg"
gen gr = 1
append using "$data/trendagg2"
replace gr = 2 if gr==.
sort year gr
gen x = _n
replace x = _n + 2 if _n >= 3
replace x = _n + 4 if _n >= 5
replace x = _n + 6 if _n >= 7
replace x = _n + 8 if _n >= 9
separate estimate, by(gr)
separate high, by(gr)
separate low, by(gr)
save "$data/pretrend_agg", replace
restore

preserve
clear 
use "$data/pretrend_agg"
twoway (scatter estimate1 x, mcolor(navy)) (rcap high1 low1 x, lcolor(navy)) ///
	   (scatter estimate2 x, msymbol(d) mcolor(maroon)) (rcap high2 low2 x, lcolor(maroon)), ///
	   ylabel(, angle(0)) scheme(lean1) xlabel(none) xtitle(" ") ///
	   ytitle("") yline(0, lcolor(black)) ///
	   text(-2.6 0.8 "2014", place(e)) ///
	   text(-2.6 4.8 "2015", place(e)) ///
	   text(-2.6 8.8 "2016", place(e)) ///
	   text(-2.6 12.8 "2017", place(e)) ///
	   text(-2.6 16.8 "2018", place(e)) ///
	   legend(order(1 "All budget" 3 "Restricted") position(1) row(1) ring(0)) ///
	   ylabel(, nogrid) graphregion(color(white))
restore
